package model.components;

//import gui.Icons;
import model.SimulatorConstants;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * SubstrateSwitch Class. Subclass of Node.
 */
public class SubstrateSwitch extends Node {

//	private List<RequestSwitch> virtualSwitches;
	private List<NodeMapping> virtualSwitches;
	
	private int maxLogicalIfaces;
	
	public SubstrateSwitch(int id) {
		super(id);
		name = "substrateSwitch"+id;
		/*setIcon(Icons.SWITCH);
		setSmallIcon(Icons.SWITCH_SMALL);
		setPickedIcon(Icons.SWITCH_PICKED);
		setYellowIcon(Icons.SWITCH_YELLOW);
		setRedIcon(Icons.SWITCH_RED);*/
		setMaxLogicalIfaces(SimulatorConstants.MAX_LOGICAl_IFACES_SWITCH);
		virtualSwitches = new CopyOnWriteArrayList<NodeMapping>();
	}

	public List<NodeMapping> getVirtualSwitches() {
		return virtualSwitches;
	}

	public void setVirtualSwitches(List<NodeMapping> virtualSwitches) {
		this.virtualSwitches = virtualSwitches;
	}
	
	public void addVirtualSwitch(NodeMapping rs) {
		this.virtualSwitches.add(rs);
	}
	
	public void removeVirtualSwitch(String reqId, int nodeId) {
		for (NodeMapping vs : virtualSwitches)
			if (vs.getRequest().getId().equals(reqId)
					&& vs.getRequestNode().getId() == nodeId)
				virtualSwitches.remove(vs);
	}

	public int getAvailableCpu() {
		int returnValue = this.getCpu();
		for (NodeMapping s : virtualSwitches)
			returnValue-=s.getRequestNode().getCpu();
		return returnValue;
	}
	
	public int getAvailableMemory() {
		int returnValue = this.getMemory();
		for (NodeMapping s : virtualSwitches)
			returnValue-=s.getRequestNode().getMemory();
		return returnValue;
	}
	
	public int getAvailableVlans(Collection<Link> links) {
		int returnValue = this.vlans;
		for (NodeMapping s : virtualSwitches)
			returnValue-=s.getRequestNode().getVlans();
		for (Link l : links)
			returnValue -= ((SubstrateLink) l).getVirtualLinks().size();
		return returnValue;
	}

	public int getMaxLogicalIfaces() {
		return maxLogicalIfaces;
	}

	public void setMaxLogicalIfaces(int maxLogicalIfaces) {
		this.maxLogicalIfaces = maxLogicalIfaces;
	}

//	public Object getCopy() {
//		SubstrateSwitch s = new SubstrateSwitch(this.getId());
//		s.name = this.name;
//		s.cpu = this.cpu;
//		s.memory = this.memory;
//		s.vlans = this.vlans;
//		s.so = this.so;
//		s.netStack = this.netStack;
//		s.location = this.location;
//		s.maxLogicalIfaces = this.maxLogicalIfaces;
////		for (Interface i : this.interfaces)
////			s.interfaces.add((Interface) i.getCopy());
//		for (RequestSwitch rs : this.virtualSwitches)
//			s.virtualSwitches.add((RequestSwitch) rs.getCopy());
//		return s;
//	}
	public Object getCopy() {
		SubstrateSwitch s = new SubstrateSwitch(this.getId());
		s.name = this.name;
		s.cpu = this.cpu;
		s.memory = this.memory;
		s.vlans = this.vlans;
		s.so = this.so;
		s.netStack = this.netStack;
		s.location = this.location;
		s.maxLogicalIfaces = this.maxLogicalIfaces;
//		for (Interface i : this.interfaces)
//			s.interfaces.add((Interface) i.getCopy());
		for (NodeMapping rs : this.virtualSwitches)
			s.virtualSwitches.add(rs.getCopy());
		return s;
	}
}
